<?php

namespace App\Http\Controllers;

use App\Models\Project;
use App\Models\Quality;
use App\Models\Safe;
use App\Models\Schedule;
use App\Models\User;
use App\Services\UserService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class UserController extends Controller
{
    protected $UserService;
    public function __construct(UserService $UserService)
    {
        $this->UserService = $UserService;
    }

    public function login(Request $Request)
    {
        $Request->validate([
            'mobile' => 'required|max:255',
            'password' => 'required|min:6',
        ]);

        $user = User::with('role')->with('projects')->where('mobile',$Request->mobile)->first();
        if(!$user){
            return response()->json(['code' => '404', 'msg' => '用户不存在']);
        }
        if(!$user->validateForPassportPasswordGrant($Request->password)){
            return response()->json(['code' => '404', 'msg' => '密码错误']);
        }

        if($user->role_id == 0){
            $projects = Project::where('company_id',$user->company_id)->get();
        }else{
            $projects = $user->projects;
        }
        $token = $user->createToken('api')->accessToken;
        return response()->json(['code' => '200', 'msg' => '登录成功','user'=>$user,'token'=>$token,'projects'=>$projects]);
    }

    public function index() {
        $data = Auth::user();
        $data->role;
        return response()->json(['code' => '200', 'msg' => '操作成功','data'=>$data]);
    }

    public function projects() {
        $user = Auth::user();
        if($user->role_id == 0){
            $projects = Project::where('company_id',$user->company_id)->get();
        }else{
            $projects = $user->projects;
        }
        return response()->json(['code' => '200', 'msg' => '操作成功','data'=>$projects]);
    }

    public function update(Request $Request) {
        if(!$Request->avatar){
            return response()->json(['code' => '403', 'msg' => '请上传头像']);
        }
        if(!$Request->password){
            return response()->json(['code' => '403', 'msg' => '请输入密码']);
        }
        $arr = [];
        $arr['avatar'] = $Request->avatar;
        $arr['password'] = $Request->password;
        $data = $this->UserService->userUpdate($arr);
        return response()->json(['code' => '200', 'msg' => '操作成功','data'=>$data]);
    }

    public function statistics(Request $Request) {
        $Three = $this->UserService->statisticsThree($Request->project_id);
        $Overdue = $this->UserService->statisticsOverdue($Request->project_id);
        $Maintenance = $this->UserService->statisticsMaintenance($Request->project_id);
        $Examine = $this->UserService->statisticsExamine($Request->project_id);
        return response()->json(['code' => '200', 'Three' => $Three, 'Overdue' => $Overdue, 'Maintenance' => $Maintenance, 'Examine' => $Examine]);
    }

    public function statisticsList(Request $Request) {
        $date = Date('Y-m-d H:i:s',time());
        $dateThree = Date('Y-m-d H:i:s',time()+3*24*3600);
        $user_id = Auth::user()->id;

        $data = [];
        $data['schedule'] = Schedule::whereDate('start_date','<=',$date)->whereDate('end_date','<=',$dateThree)->where('project_id',$Request->project_id)->where('person_id',$user_id)->get();
        $data['safe'] = Safe::with('type')->where('project_id',$Request->project_id)->where('person_id',$user_id)->get();
        $data['quality'] = Quality::with('type')->whereDate('start_date','<=',$date)->whereDate('end_date','<=',$date)->where('project_id',$Request->project_id)->where('person_id',$user_id)->get();
        return response()->json(['code' => '200', 'data' => $data]);
    }

   
}
